package com.sudo.service.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.sudo.common.utils.LocalDateTimeUtil;
import com.sudo.common.utils.PagedGridResult;
import com.sudo.dao.admin.entity.SysUserLoginRecord;
import com.sudo.dao.admin.mapper.SysUserLoginRecordMapper;
import com.sudo.dao.admin.model.bo.SysUserLoginRecordBO;
import com.sudo.starter.ip2region.pojo.IpInfo;
import com.sudo.starter.ip2region.strategy.Ip2RegionStrategy;
import com.sudo.service.admin.service.AdminBaseService;
import com.sudo.service.admin.service.SysUserLoginRecordService;
import com.sudo.starter.threadpool.annotation.AsyncTask;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 为道日损 2020-06-19 22:27
 * webSite：https://www.sudo.ren
 */
@Slf4j
@Service
public class SysUserLoginRecordServiceImpl extends AdminBaseService implements SysUserLoginRecordService {

    @Resource
    private SysUserLoginRecordMapper sysUserLoginRecordMapper;

    @Resource
    private Ip2RegionStrategy ip2RegionStrategy;

    @AsyncTask
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public Integer addUserLoginRecord(String username, String userAgent,String ip) {
        SysUserLoginRecord sysUserLoginRecord = new SysUserLoginRecord();
        //转成UserAgent对象
        UserAgent ua = UserAgent.parseUserAgentString(userAgent);
        //浏览器信息
        Browser browser = ua.getBrowser();
        //系统信息
        OperatingSystem os = ua.getOperatingSystem();
        //系统名称
        String systemName = os.getName();
        //设备
        String device = os.getDeviceType().getName();
        //浏览器
        String browserInfo = browser.getName() + "_" + ua.getBrowserVersion().getVersion();
        //
        String search = ip2RegionStrategy.search(ip);
        if (StringUtils.isNotBlank(search)) {
            IpInfo ipInfo = new IpInfo(search);
            sysUserLoginRecord.setCountry(ipInfo.getCountry());
            sysUserLoginRecord.setProvince(ipInfo.getProvince());
            sysUserLoginRecord.setCity(ipInfo.getCity());
        }

        sysUserLoginRecord.setUsername(username);
        sysUserLoginRecord.setSource(ip);
        sysUserLoginRecord.setDevice(device);
        sysUserLoginRecord.setSystemName(systemName);
        sysUserLoginRecord.setBrowser(browserInfo);
        sysUserLoginRecord.setLoginTime(LocalDateTimeUtil.getStandardDateTimeStr());
        log.info("用户【{}】登录成功",username);
        return sysUserLoginRecordMapper.insertSelective(sysUserLoginRecord);
    }

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    @Override
    public PagedGridResult queryUserLoginRecordByPage(SysUserLoginRecordBO sysUserLoginRecordBO) {
        PageHelper.startPage(sysUserLoginRecordBO.getPage(), sysUserLoginRecordBO.getLimit());
        Example example = new Example(SysUserLoginRecord.class);
        example.orderBy(SysUserLoginRecord.Field.id).desc();
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(sysUserLoginRecordBO.getUsername())) {
            criteria.andEqualTo(SysUserLoginRecord.Field.username,sysUserLoginRecordBO.getUsername());
        }
        if (StringUtils.isNotBlank(sysUserLoginRecordBO.getStartDate())) {
            criteria.andGreaterThanOrEqualTo(SysUserLoginRecord.Field.loginTime,sysUserLoginRecordBO.getStartDate());
        }
        if (StringUtils.isNotBlank(sysUserLoginRecordBO.getEndDate())) {
            criteria.andLessThanOrEqualTo(SysUserLoginRecord.Field.loginTime,sysUserLoginRecordBO.getEndDate());
        }
        List<SysUserLoginRecord> sysUserBehaviorList = sysUserLoginRecordMapper.selectByExample(example);
        return setterPagedGrid(sysUserBehaviorList, sysUserLoginRecordBO.getPage());
    }
}
